package uk.ac.lkl.common.ui;



import javax.swing.*;


/**
 * A view of an instance of class O.
 * 
 * This class is an abstraction of the fundamental nature of a view of some type
 * of 'model' of type O. Firstly, the view extends JPanel. Secondly, the model
 * that the view is based on can change dynamically.
 * 
 * @param O
 *            the class of the instance this is a view of
 * 
 * @author Darren Pearce
 * @version $Revision$
 * @version $Date$
 * 
 */
public abstract class View<O> extends JPanel {

    /**
     * The model this view is currently based on.
     * 
     */
    private O model;


    /**
     * Create a new instance based on the given model.
     * 
     * @param model
     *            the model of this instance
     */
    public View(O model) {
        this.model = model;
    }


    /**
     * 
     * Set the model of this instance.
     * 
     * An IllegalArgumentException is thrown if the model argument is null. i.e.
     * the view must always have a valid model.
     * 
     * Note that the view is updated regardless of whether the new model is
     * equivalent (under ==) to the current model. This is because the cell's
     * editability and other attributes may need to be changed as a result of
     * the model change.
     * 
     * @param model
     *            the new model for this instance
     * 
     */
    public void setModel(O model) {
        this.model = model;

        updateView();
    }


    /**
     * Update the view so that it is consistent with the current model.
     * 
     */
    public abstract void updateView();


    /**
     * Get the model of this instance.
     * 
     * @return the model
     * 
     */
    public O getModel() {
        return model;
    }

}
